<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

// TODO Remove this -- for debugging
error_reporting(E_ALL);

// necessary for session variables in J1.0
session_start();

// configuration
DEFINE("_HELPDESK_DSCVERSION", '1.01');
DEFINE("_HELPDESK_DSCCOPYRIGHTYEAR", '2008');
if (!defined("_THISCOMPONENT")) { DEFINE("_THISCOMPONENT", 'com_helpdesk');}

DEFINE("_THISCOMPONENTNAME", substr ( _THISCOMPONENT, 4 ) );

// base definitions
if (!defined("_JEXEC")) { DEFINE('_JEXEC', '1'); }

if (!defined("JURI_BASE")) { DEFINE('JURI_BASE', $mosConfig_live_site); }
//if (!defined("JPATH_SITE")) { DEFINE('JPATH_SITE', JPATH_BASE); }
if (!defined("JPATH_ADMINISTRATOR")) { DEFINE('JPATH_ADMINISTRATOR', JPATH_BASE.DS.'administrator'); }
DEFINE('JPATH_COMPONENT', JPATH_BASE.DS.'components'.DS._THISCOMPONENT);
DEFINE('JPATH_COMPONENT_SITE', JPATH_BASE.DS.'components'.DS._THISCOMPONENT);
if (!defined("JPATH_COMPONENT_ADMINISTRATOR")) { DEFINE('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR.DS.'components'.DS._THISCOMPONENT); }


// base URI definitions
DEFINE('JURI_DS', '/');
DEFINE('JURI_ADMINISTRATOR', JURI_BASE.JURI_DS.'administrator');
DEFINE('JURI_COMPONENT', JURI_BASE.JURI_DS.'components'.JURI_DS._THISCOMPONENT);
DEFINE('JURI_COMPONENT_SITE', JURI_BASE.JURI_DS.'components'.JURI_DS._THISCOMPONENT);
DEFINE('JURI_COMPONENT_ADMINISTRATOR', JURI_ADMINISTRATOR.JURI_DS.'components'.JURI_DS._THISCOMPONENT);

if (file_exists(JPATH_COMPONENT_SITE.DS.'languages'.DS.$mosConfig_lang.'.php')) {
      include_once(JPATH_COMPONENT_SITE.DS.'languages'.DS.$mosConfig_lang.'.php');
} else {
      include_once(JPATH_COMPONENT_SITE.DS.'languages'.DS.'english.php');
}

// load component classes
	require_once(JPATH_COMPONENT_SITE.DS._THISCOMPONENTNAME.'.class.php');

// Require the base controller
	require_once( JPATH_COMPONENT_SITE.DS.'controller.php' );
	
// Require Helpers
	$helpersPath = JPATH_ADMINISTRATOR.DS.'components'.DS._THISCOMPONENT.DS.'helpers';
	$dh  = opendir($helpersPath);
	while (false !== ($filename = readdir($dh))) {
		$fullpath = $helpersPath.DS.$filename;
		$extension = pathinfo( $fullpath, PATHINFO_EXTENSION );
		if (!is_dir( $fullpath ) && file_exists( $fullpath ) && ($extension == 'php') ) { 
			require_once( $fullpath ); 
		}
	}
	
// Require specific controller if requested
	if($controller = HelpDeskRequest::getVar( 'controller', HelpDeskRequest::getVar( 'section', strtolower('HelpDesk') ) ) ) {
		$controller = strtolower( $controller );
		HelpDeskRequest::setVar( 'section', $controller );
		$path = JPATH_COMPONENT_SITE.DS."controllers".DS."{$controller}.php";
		if (file_exists($path)) {
			require_once $path;
		} else {
			$controller = '';
		}
	}

// include css and js files	
	$no_html = HelpDeskRequest::getVar( 'no_html' );
	if (!$no_html)
	{
		echo "<link rel='stylesheet' type='text/css' href='".JURI_COMPONENT_SITE.JURI_DS."includes".JURI_DS."css".JURI_DS._THISCOMPONENTNAME.".css'>";
		echo "<link rel='stylesheet' type='text/css' href='".JURI_COMPONENT_SITE.JURI_DS."includes".JURI_DS."css".JURI_DS."modal.css'>";
		echo "<script language='javascript' type='text/javascript' src='".JURI_COMPONENT_SITE.JURI_DS."includes".JURI_DS."js".JURI_DS._THISCOMPONENTNAME.".js'></script>";
		echo "<script language='javascript' type='text/javascript' src='".JURI_COMPONENT_SITE.JURI_DS."includes".JURI_DS."js".JURI_DS."mootools.js'></script>";
		echo "<script language='javascript' type='text/javascript' src='".JURI_COMPONENT_SITE.JURI_DS."includes".JURI_DS."js".JURI_DS."modal.js'></script>";
	}
	
	// Create the controller
	$classname    = 'HelpDeskController'.$controller;
	$controller   = new $classname( );
	
	// load the plugins
	HelpDeskHelperPlugin::importPluginGroup( strtolower( 'HelpDesk' ) );

	// Perform the requested task
	$controller->execute( HelpDeskRequest::getVar( 'task' ) );
		
	// Redirect if set by the controller
	$controller->redirect();